/* << fastinsert report library >> OciDemo.cpp  Nov 1, 2010
 *
 * 	http://code.google.com/p/libfir
 *
 * Copyright (c) 2005 Pavlo Drobov (drobov.pavel@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

#include <stdio.h>
#include <argtable2.h>
#include "Debug.h"
#include "OciBase.h"

#include "GridBase.h"

using namespace dasco::report;

static const char * sql[]
{
	"select * from v$version"
	,"SELECT OBJECT_ID id, OBJECT_NAME name, OBJECT_TYPE type FROM user_objects"
	,"select 1,2  from dual"
	,NULL
};


static void test1(const OciConnect& conn, const char * savePathDir) throw (OciException)
{
	unsigned qnt;
	IGrid** grids;

	//init PDF document
	PdfBase * pdf = new PdfBase("oci demo");

	//init OCI document
	OciBase * ociBase = new OciBase(conn);

	//make multiload SQL
	ociBase->load(sql,&grids,&qnt);

	//render grids
	for(unsigned i=0; i<qnt;++i)
		pdf->renderGrid(grids[i]);

	//free memory for grids
	ociBase->free(grids,qnt);

	//delete OCI document
	delete ociBase;

	//save PDF document
	char buf[0x1FF];
	::sprintf(buf,"%s/oci_out.pdf",savePathDir);

	pdf->numerationPage("Page");
	pdf->currentDate();
	pdf->setFIRdemo();
	pdf->save(buf);
	delete pdf;
}


int main(int argc, char **argv)
{
	struct arg_lit *help = arg_lit0(NULL, "help", "print this help and exit");
	struct arg_str *host = arg_str0("h", "host", "localhost", "oracle host");
	struct arg_int *port = arg_int0("p", "port", NULL, "oracle port");
	struct arg_str *sid = arg_str0("s", "SID", NULL, "oracle sid");
	struct arg_str *user = arg_str0("U", "user", NULL, "oracle user");
	struct arg_str *passwd = arg_str0("W", "password", NULL, "oracle password");
	struct arg_str *out_dir = arg_str0("o", "out_dir", NULL, "output dir (default .)");
	struct arg_end *end = arg_end(20);

	void* argtable[] = { help, host, port, sid, user, passwd, out_dir,end };

	if (::arg_parse(argc, argv, argtable) > 0)
	{
		::arg_print_errors(stdout, end, argv[0]);
		::printf("Try '%s --help' for more information.\n", argv[0]);
		return -1;
	}

    if (argc ==1 || help->count > 0)
    {
             ::arg_print_syntax(stdout,argtable,"\n");
             ::arg_print_glossary(stdout,argtable,"  %-25s %s\n");
             ::arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
             return -1;
    }

    char buf[0x1FF];
    ::sprintf(buf,"//%s:%i/%s"
    		, host->count ? host->sval[0] : "localhost"
    		, port->count ? port->ival[0] : 1521
    		, sid->count ?  sid->sval[0] : "ORCL");

    OciConnect conn(user->count ? user->sval[0] : "scott",passwd->count ? passwd->sval[0] : "tiger" ,buf);
	//DEBUG_MSG("conn: " << conn);


    try
    {
    	test1(conn,out_dir->count ? out_dir->sval[0] : ".");
	} catch (OciException& ex)
	{
		::printf("error: %s : (%i) \n",ex.text, ex.number);
	}

	::arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));

	return 0;
}
